package com.gulimall.order.listener;


import com.common.to.skuillOrderTo;
import com.gulimall.order.entity.OrderEntity;
import com.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@RabbitListener(queues = "order.seckill.order.queue")
@Service
public class OrderSeckillListener {

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void listener(skuillOrderTo entity, Channel channel, Message message) throws IOException {
        System.out.println("收到秒杀订单信息，准备创建订单" + entity.getOrderSn());
        try {
            orderService.createSeckillOrder(entity);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception e){
            //有异常拒签
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }

    }
}
